package com.kintreda.ieco_server.module.api.web;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.bean.goods.Places;
import com.kintreda.ieco_server.bean.goods.Sack;
import com.kintreda.ieco_server.bean.order.BuyRecord;
import com.kintreda.ieco_server.bean.order.UsageRecord;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.order.service.*;
import com.kintreda.ieco_server.util.DateUtil;
import com.kintreda.ieco_server.util.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Transient;
import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.web
 * @Author ZhongYao_H
 * @Date 2019/4/29:15:00
 * @Year 2019 Month 04 Day 29
 * @Date_zh 2019 年 四月 星期一
 */
@Controller
@RequestMapping("/api/order")
public class ApiOrderController extends BaseController {
        @Autowired
        private IRecycleRecordService recycleRecordService;
        @Autowired
        private IUserService userService;
        @Autowired
        private IUsageRecordService usageRecordService;
        @Autowired
        private IBuyRecordService buyRecordService;
        @Autowired
        private IPlacesService placesService;
        @Autowired
        private ISackService sackService;

        @RequestMapping(value = "/recylist",method = RequestMethod.POST)
        @ResponseBody
        public Object getlist(String openid){
            String testid =  userService.findUserByOpenId(openid).getId();
            List<Map<String, Object>> myRecycleRecord = recycleRecordService.getMyRecycleRecord(testid);
            return myRecycleRecord;

        }


    /**
     * 查询订单根据状态
     *
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object queryByStatus(String openid,String status){
        String userId = userService.findUserByOpenId(openid).getId();
        return usageRecordService.queryByStatus(status,userId);
    }


    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @Transient
    public Object update(String usaId,String status){
        try{
            UsageRecord usageRecord = usageRecordService.get(usaId);
            usageRecord.setStatus(status);
            usageRecord.setUpdateDate(DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MM_SS));
            usageRecordService.update(usageRecord);
            if (status.equals("-1")){
                Sack sack = sackService.getSackByQrCode(usageRecord.getQrCode());
                sack.setStatus("0");
                sackService.update(sack);
            }
            return R.ok("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("系统异常稍后重试!");
        }


    }

    @RequestMapping(value = "/buysack",method = RequestMethod.POST)
    @ResponseBody
    public Object buySack(String openid,String num){

        try {

            User user = userService.findUserByOpenId(openid);
            Places places ;
            if (user.getType().equals("2")){
                places = placesService.getByOrgId(user.getId(),"0");

            }else {
                places = placesService.getByOrgId(user.getId(),"1");
            }

            if (places == null){
            places = new Places();
                places.setNum("0");
                places.setOrgId(user.getId());
                if (user.getType().equals("2")){
                    places.setType("0");

                }else {
                    places.setType("1");
                }
                placesService.save(places);
            }


            BuyRecord buyRecord = new BuyRecord();
            buyRecord.setMoney(Integer.parseInt(num) * Integer.parseInt(Parameters.SACK_PRICE.toString())/100+"");
            buyRecord.setNum(num);
            buyRecord.setUserOrOrgId(user.getId());
            buyRecord.setSackId("");
            buyRecordService.save(buyRecord);

            Integer n1 = Integer.parseInt(places.getNum());
            Integer n2 = Integer.parseInt(num);
            Integer n3 = n2+n1;
            places.setNum(n3+"");
            placesService.update(places);

            return R.ok("购买成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("登录失效!");
        }

    }

}
